const { no, ok } = require('../../utils/utlis');
const chatService = require('../../services/client/chatService');
const JWT = require('../../utils/jwt');
const { cosUpload, cosBatchUpload } = require('../../utils/oss');

const ossDirectory = 'chat';

module.exports = {
	chatUserList: async (req, res, next) => {
		try {
			let token = JWT.decrypt(req.headers.token);

			let userId = token.id;

			let list = await chatService.chatUserList(userId);
			res.send(ok(list));
		} catch (error) {
			res.send(no(error.message));
		}
	},
	chatByIdList: async (req, res, next) => {
		try {
			let { mode, fromId, toId } = req.body;

			let list = await chatService.chatByIdList(mode, fromId, toId);
			res.send(ok(list));
		} catch (error) {
			res.send(no(error.message));
		}
	},
	chatUploadImg: async (req, res, next) => {
		let { filename } = req.body;
		// 上传oss
		let { Location } = await cosUpload(ossDirectory, filename);
		fullUrl = 'https://' + Location;
		res.send(
			ok({
				fullUrl,
				url: '/uploads/' + filename //本地相对路径
			})
		);
	}
};
